package com.by.controller;

import com.by.core.bean.ResultBean;
import com.by.core.controller.BaseController;
import com.by.bean.ShopCartItem;
import com.by.service.ShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
@Api(tags = "购物车")
public class ShopCartController extends BaseController {

    @Autowired
    ShopCartService shopCartService;



    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public ResultBean addToCart(@RequestParam String userId, @RequestBody List<ShopCartItem> items) {
        shopCartService.addToCart(userId, items);
        return success("保存成功");
    }

    @PostMapping("/remove")
    @ApiOperation("清空购物车")
    public ResultBean removeFromCart(@RequestParam String userId) {
       shopCartService.removeFromCart(userId);
       return success("删除成功");
    }

    @GetMapping("/get")
    @ApiOperation("获取")
    public ResultBean getCartItems(@RequestParam String userId) {
        List<ShopCartItem> cartItems = shopCartService.getCartItems(userId);
        return success("获取成功!", cartItems);
    }

}
